/*Copyright (C) 2009 Careil Baptiste

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include <stdlib.h>
#include "../headers.h"
#include "Dames_constantes.h"
#include "Dames_deplacements.h"

void Dames_Move(DAMES_CASEVALUE array[DAMES_ARRAY_SIZE][DAMES_ARRAY_SIZE], DAMES_POS *pos, int *nbPionsAdverses) {
	int delta = pos->select.x - pos->dest[0].x;
	if(pos->destLen == 1 && (delta == 1 || delta == -1)) {
		//cas d'un déplacement
		DAMES_CASEVALUE element = Dames_GetPion(array, pos->select);
		Dames_Deplacer(array, element, pos->select, pos->dest[0]);
	}
	else {
		//cas d'un 'mangeage'
		Dames_Manger(array, pos->select, pos->dest[0]);
		*nbPionsAdverses--;
		if(pos->destLen > 1) {
			unsigned int i;
			for(i = 1; i < pos->destLen; i += 2) {
				Dames_Manger(array, pos->dest[i], pos->dest[i+1]);
				*nbPionsAdverses--;
			}
		}
	}
}

void Dames_Manger(DAMES_CASEVALUE array[DAMES_ARRAY_SIZE][DAMES_ARRAY_SIZE], IPoint src, IPoint dest) {
	IPoint ouManger;
	DAMES_CASEVALUE elementAManger, elementMangeant;
	ouManger.x = (src.x + dest.x) / 2;
	ouManger.y = (src.y + dest.y) / 2;
	elementAManger = Dames_GetPion(array, ouManger);
	elementMangeant = Dames_GetPion(array, src);

	Dames_Enlever(array, elementAManger, ouManger);
	Dames_Deplacer(array, elementMangeant, src, dest);
}

bool Dames_SelectValid(DAMES_CASEVALUE array[DAMES_ARRAY_SIZE][DAMES_ARRAY_SIZE], IPoint select, bool whitePlayerTurn) {
	bool retVal = false;
	if(whitePlayerTurn) {
		if(Dames_GetCouleurPion(array, select) & WHITE) {
			retVal = true;
		}
	}
	else {
		if(Dames_GetCouleurPion(array, select) & BLACK) {
			retVal = true;
		}
	}
	return retVal;
}

bool Dames_DestValid(DAMES_CASEVALUE array[DAMES_ARRAY_SIZE][DAMES_ARRAY_SIZE], DAMES_POS *pos, bool whitePlayerTurn) {
	bool retVal = false;
	if(Dames_GetPion(array, pos->select) == EMPTY && !Dames_IsCaseBlancheIP(array, pos->select)) {
		//si la destination est vide de pion et n'est pas une case blanche
		if(pos->destLen == 0) {
			//encore aucune detination
			int delta = pos->curseur.y - pos->select.y;
			if(whitePlayerTurn) {
				//tour du joueur blanc (en bas, fin du tableau)
				if(delta == 1) {
					//déplacement
					retVal == true;
				}
				else if(delta == 2 || delta == -2) {
					IPoint pt;
					pt.x = (pos->curseur.x - pos->select.x) / 2;
					pt.y = (pos->curseur.y - pos->select.y) / 2;
					if(Dames_GetCouleurPion(array, pt) == BLACK) {
						retVal = true;
					}
				}
				else if(delta > 2 || delta < -2) {
					//si le déplacement est 'long'
					DAMES_CASEVALUE pionDeplace = Dames_GetPion(array, pos->curseur);
					if(pionDeplace & DAME) {
						//si le pion déplacée est une dame
						//
					}
				}
			}
			else {
				//tour du joueur noir (en haut, debut du tableau)
				if(delta == -1) {
					retVal == true;
				}
				else if(delta == -2 || delta == 2) {
					IPoint pt;
					pt.x = (pos->curseur.x - pos->select.x) / 2;
					pt.y = (pos->curseur.y - pos->select.y) / 2;
					if(Dames_GetCouleurPion(array, pt) == WHITE) {
						retVal = true;
					}
				}
			}
		}
		else if(pos->destLen == 1) {
			//s'il y a déjà une seule destination
			int delta1 = pos->dest[0].x - pos->select.x;
			int delta2 = pos->curseur.x - pos->dest[0].x;

			if(delta1 == 2 || delta1 == -2) {
				//premier déplacement est un 'mangeage'
				if(delta2 == 2 || delta2 == -2) {
					//deuxième déplacement est un 'mangeage'
					IPoint pt;
					pt.x = (pos->curseur.x + pos->dest[0].x) / 2;
					pt.y = (pos->curseur.y + pos->dest[0].y) / 2;
					if(whitePlayerTurn) {
						// tour du joueur blanc
						if(Dames_GetCouleurPion(array, pt) == BLACK) {
							//le pion 'mangé' est noir
							retVal = true;
						}
					}
					else {
						//tour du joueur noir
						if(Dames_GetCouleurPion(array, pt) == WHITE) {
							//le pion 'mangé' est blanc
							retVal = true;
						}
					}
				}
			}
		}
		else {
			//s'il y a déjà plusieurs destination
			int delta = pos->curseur.x - pos->dest[pos->destLen-1].x;
			if(delta == 2 || delta == -2) {
				//cas d'un 'mangeage'
				IPoint pt;
				pt.x = (pos->curseur.x + pos->dest[pos->destLen-1].x) / 2;
				pt.y = (pos->curseur.y + pos->dest[pos->destLen-1].y) / 2;
				if(whitePlayerTurn) {
					//tour du joueur blanc
					if(Dames_GetCouleurPion(array, pt) == BLACK) {
						//le pion 'mangé' est noir
						retVal = true;
					}
				}
				else {
					//tour du joueur noir
					if(Dames_GetCouleurPion(array, pt) == WHITE) {
						//le pion 'mangé' est blanc
						retVal = true;
					}
				}
			}
		}
	}
	return retVal;
}

